using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace smartBrowser.Bookmark
{   
    /// <summary>
    /// @author Joel
    /// </summary>  
    public class BookmarkList
    {
        private ArrayList bookmarks;
        private ArrayList newBookmarks;


        public BookmarkList()
        {

            this.bookmarks = new ArrayList();
            this.newBookmarks = new ArrayList();

        }


        /// <summary>
        /// Load the bookmark list from the file where he's saved
        /// </summary>
        public void OpenBookmarkFile()
        {
            XmlDocument doc = new XmlDocument();
            XmlTextReader reader;            
            ArrayList tags = null;
            String label = "";
            String link = "";

            try
            {
                reader = new XmlTextReader(Path.Combine("Configuration", "Bookmarks.xml"));
                reader.WhitespaceHandling = WhitespaceHandling.None;
                doc.Load(reader);

                XmlNode bookmarkList = doc.FirstChild;
                while (!bookmarkList.Name.Equals("BookmarkList"))
                {
                    bookmarkList = bookmarkList.NextSibling;
                }


                foreach (XmlNode bookmark in bookmarkList.ChildNodes)
                {
                    foreach (XmlAttribute attr in bookmark.Attributes)
                    {
                        if (attr.Name.Equals("Label"))
                        {
                            label = attr.Value;
                        }

                        if (attr.Name.Equals("Link"))
                        {                            
                            link = attr.Value;
                        }

                        if(attr.Name.Equals("Tags"))
                        {
                            String[] array = attr.Value.Split(';');
                            tags = new ArrayList();

                            for(int i = 0; i < array.Length; i++)
                            {
                                tags.Add(array[i]);
                            }

                        }

                    }

                    this.bookmarks.Add(new Bookmark(link, label, tags));                    
                }
                
                reader.Close();               
            }
            catch (Exception)
            {
                this.CreateBookmarksFile();
            }                                    
            
        }


        /// <summary>
        /// Save a give bookmark to file
        /// </summary>
        /// <param name="link">the string with the link</param>
        /// <param name="label">the string with the label</param>
        /// <param name="tags">the arraylist with the tags</param>
        public void SaveBookmarkToFile(Bookmark bookmark)
        {
            XmlDocument doc = new XmlDocument();
            XmlTextReader reader;

            try
            {
                reader = new XmlTextReader(Path.Combine("Configuration", "Bookmarks.xml"));
                reader.WhitespaceHandling = WhitespaceHandling.None;
                doc.Load(reader);
                reader.Close();

                XmlNode bookmarkList = doc.FirstChild;
                while (!bookmarkList.Name.Equals("BookmarkList"))
                {
                    bookmarkList = bookmarkList.NextSibling;
                }

                XmlElement newBookmark = doc.CreateElement("Bookmark");
                newBookmark.SetAttribute("Label", bookmark.Label);
                newBookmark.SetAttribute("Link", bookmark.Link.AbsoluteUri);
                String allTags = "";

                foreach (String tag in bookmark.TagList)
                {
                    allTags = allTags + tag + ";";
                }

                newBookmark.SetAttribute("Tags", allTags);

                bookmarkList.AppendChild(newBookmark);
            }
            catch (Exception)
            {
                MessageBox.Show("Error trying save the bookmark to file..");          
            }

            try
            {
                doc.Save(Path.Combine("Configuration", "Bookmarks.xml"));
                MessageBox.Show("Bookmark Link: Successfull.");
            }
            catch (Exception e)
            {
                MessageBox.Show("ERROR: " + e.Message);
            }
            
        }


        /// <summary>
        /// If first time, the file doesn't already exist
        /// </summary>
        public void CreateBookmarksFile()
        {
            DirectoryInfo directory = new DirectoryInfo("Configuration");
            
            if (!directory.Exists)
                directory.Create();

            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration decl = xmlDoc.CreateXmlDeclaration("1.0", "ISO-8859-1", "");
            xmlDoc.AppendChild(decl);
            XmlElement elem = xmlDoc.CreateElement("BookmarkList");
            xmlDoc.AppendChild(elem);

            try
            {
                xmlDoc.Save(Path.Combine("Configuration", "Bookmarks.xml"));

            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Creating: " + e.Message);
            }

            this.OpenBookmarkFile();
        }

        public void addBookmark(Bookmark bookmark)
        {
            this.newBookmarks.Add(bookmark);
            this.SaveBookmarkToFile(bookmark);
        }

        public ArrayList Bookmarks
        {            
            get
            {
                ArrayList tempBookmarkList = new ArrayList();


                tempBookmarkList.AddRange(this.bookmarks);
                tempBookmarkList.AddRange(this.newBookmarks);

                return tempBookmarkList;
            }
        }
    }
}
